
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/15/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfprojectaccept.dao;

import java.util.List;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.manager.commons.CommonUtil;
import com.manager.commons.page.PageBean;
import org.apache.log4j.Logger;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
import com.application.yfprojectaccept.entity.YfProjectAccept;
import com.application.yfprojectaccept.struts.form.YfProjectAcceptForm;

/**
 * 
 * Description:YfProjectAcceptDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/15/2012
 * 
 */
public class YfProjectAcceptDAO extends HibernateGenericDao<YfProjectAccept> {
	private static final Logger logger = Logger
			.getLogger(YfProjectAcceptDAO.class);

	private YfProjectAccept yfProjectAccept;

	public YfProjectAccept getYfProjectAccept() {
		return this.yfProjectAccept;
	}

	public void setYfProjectAccept(YfProjectAccept yfProjectAccept) {
		this.yfProjectAccept = yfProjectAccept;
	}

	public Integer getAllCountYfProjectAccept(
			YfProjectAcceptForm yfProjectAcceptForm) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT COUNT(0) FROM yf_project_accept t left join yf_project p on t.project_no = p.project_no left join department b on p.project_org = b.id  WHERE 1=1 ");
		sql = getStringBuffer(yfProjectAcceptForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}

	public void deleteYfProjectAccepts(YfProjectAcceptForm yfProjectAcceptForm) {
		String hql = " delete from YfProjectAccept where id = '"+ yfProjectAcceptForm.getId() + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfProjectAccepts failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfProjectAccept(YfProjectAcceptForm yfProjectAcceptForm,
			PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT t.*,p.*,b.name project_org_name FROM yf_project_accept t left join yf_project p on t.project_no = p.project_no left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfProjectAcceptForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(
			YfProjectAcceptForm yfProjectAcceptForm, StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfProjectAcceptForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfProjectAcceptForm.getProjectNo().trim() + "%");
		}
		if (null != yfProjectAcceptForm.getAuditStatus()
				&& yfProjectAcceptForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfProjectAcceptForm.getAuditStatus());
		}
		if (StringUtils.isNotBlank(yfProjectAcceptForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfProjectAcceptForm.getId());
		}
		if (StringUtils.isNotBlank(yfProjectAcceptForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfProjectAcceptForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfProjectAcceptForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfProjectAcceptForm.getProjectOrg());
		}
		if (null != yfProjectAcceptForm.getProjectType()&& yfProjectAcceptForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfProjectAcceptForm.getProjectType());
		}
		if(StringUtils.isBlank(yfProjectAcceptForm.getId())){
			buf = CommonUtil.filterDataYanShou(buf,args);
		}
		return buf;
	}

}
